<?php
    function ApplicationRegister($data)
    {
        $sql = "INSERT INTO c_application_register SET 
            `userId`='" . $data["userId"] . "'
            , `refCode`='" . $data["refCode"] . "'
            , `manufacturer`='" . strtoupper($data["branch"]) . "'
            , `handset`='" . strtoupper($data["handset"]) . "'
            , `midp`='" . $data["midp"] . "'
            , `width`='" . $data["w"] . "'
            , `height`='" . $data["h"] . "'
            , `os_type`='" . $data["os_type"] . "'
            , `operation_system`='" . $data["operation_system"] . "'
            , `type_content`=" . $data["type_content"] . "
            , `type`=1
            , `create_date`=" . $data["create_date"] . "            
            , `day`=" . date("d") . "
            , `month`=" . date("m") . "
            , `year`=" . date("Y") . "
        ";
        $result = mysql_query($sql);
        $appRegisterId = mysql_insert_id();
       
        return $appRegisterId;
    }
    
    function UpdateApplicationView($data)
    {
        $sql = "INSERT INTO c_application_view SET 
            `appId`='" . $data["appId"] . "'
            , `manufacturer`='" . strtoupper($data["branch"]) . "'
            , `handset`='" . strtoupper($data["handset"]) . "'
            , `midp`='" . $data["midp"] . "'
            , `width`='" . $data["w"] . "'
            , `height`='" . $data["h"] . "'
            , `os_type`='" . $data["os_type"] . "'
            , `operation_system`='" . $data["operation_system"] . "'
            , `type_content`=" . $data["type_content"] . "            
            , `create_date`=" . time() . "            
            , `day`=" . date("d") . "
            , `month`=" . date("m") . "
            , `year`=" . date("Y") . "
        ";
        $result = mysql_query($sql);
        return $result;    
    }

    function getUser($refCode)
    {
        $sql = "SELECT id, username FROM c_user WHERE refCode='" . mysql_escape_string($refCode) . "'";
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows;   
    }
    
    function checkVersion($type, $version)
    {
        $sql = "SELECT isNew FROM c_version WHERE `version`='" . $version . "' AND `type`='" . $type . "'";        
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows;    
    }
    
    function GetAllCategory($type)
    {
        $sql = "SELECT id, name FROM c_category WHERE type=" . $type . " ORDER BY order_cat ASC";        
        $result = @mysql_query($sql);
        $rows = array();
        while($row = @mysql_fetch_assoc($result)){
            $rows[] = $row;            
        }       
        return $rows;
    }
    
    function checkActive($data)
    {
        $sql = "SELECT id FROM c_application_active WHERE `appRegisterId`='" . $data["appId"] . "' AND `level`='" . $data["level"] . "'"; 
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows;    
    }
    
    function getLastestActive($data)
    {
        $sql = "SELECT id, create_date, level FROM c_application_active WHERE `appRegisterId`=" . $data["appId"] . " ORDER BY create_date DESC LIMIT 1";          
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows;    
    }
    
    function getAdv()
    {
        $sql = "SELECT * FROM c_adv WHERE type=0 AND dynamic=0 AND status=1 ORDER BY create_date LIMIT 1";
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows;   
    }
    
    function getPromotion($type, $os_type)
    {
        $sql = "SELECT * FROM c_adv WHERE type=0 AND dynamic=1 AND status=1 AND box='" . $type . "' AND os_type='" . mysql_escape_string($os_type) . "' ORDER BY create_date LIMIT 1";
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows;   
    }
    
    function getAdvText()
    {
        $sql = "SELECT * FROM c_adv WHERE type=1 AND dynamic=0 AND status=1 ORDER BY create_date LIMIT 1";
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows;   
    }
    
    function getPromotionText($type, $os_type)
    {
        $sql = "SELECT * FROM c_adv WHERE type=1 AND dynamic=1 AND status=1 AND box='" . mysql_escape_string($type) . "' AND os_type='" . mysql_escape_string($os_type) . "' ORDER BY create_date LIMIT 1";
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows;   
    }
    
    function UpdateView($dataId, $table)
    {
        $sql = "UPDATE " . $table . " SET hit=(hit+1) WHERE id=" . $dataId;
        $result = mysql_query($sql);                 
        return $result;    
    }  
    
    function cleanQuery($string)
    {        
        if(empty($string)) return $string;
        $string = mysql_escape_string(trim($string));
        
        $badWords = array(
            "/Select(.*)From/i"
            , "/Union(.*)Select/i"
            , "/Update(.*)Set/i"
            , "/Delete(.*)From/i"
            , "/Drop(.*)Table/i"
            , "/Insert(.*)Into/i"                
            , "/http/i"
            , "/--/i"
        );
        
        $string = preg_replace($badWords, "", $string);

        return $string;
    }
    
    function getImage($nameImage, $folder, $date='', $type='')
    {        
        if($type!='')
            $nameImage=$type.'_'.$nameImage;
        if($date!='')
            return baseUrl() . "upload/" . $folder . "/" . date("Y", $date) . "/" . date("md", $date) . "/" . $nameImage;
        else
            return baseUrl() . "upload/" . $folder . "/" . $nameImage;
    }
    
    function OpType()
    {
        return array(
            "j2me"=>1
            , "android"=>2
            , "ios"=>3
            , "blackberry"=>4
        );
    }
    
    /*
    * Lấy dữ liệu video 
    */
    
    function GetAllVideo($data)
    {
        $startRecord = ($data["p"] - 1) * $data["record"];
        $condition = "";
        if(!empty($data["keyword"])){
            $keyword = str_replace(" ", "%", $data["keyword"]);
            $condition .= " AND title LIKE ('%" . mysql_escape_string($keyword) . "%')";
        }         
        $condition .= !empty($data["categoryId"]) ? " AND categoryId=" . $data["categoryId"] : "";
        $condition .= " AND (file_mp4 is not null OR file_mp4!='')";
        $condition .= " AND status=1";
        
        $sql = "SELECT id, title, introtext, download, picture, create_date, file, file_mp4, duration FROM c_video WHERE 1 " . $condition . " ORDER BY create_date DESC LIMIT " . $startRecord . ", " . $data["record"];                        
        $result = @mysql_query($sql);
        $rows = array();
        while($row = @mysql_fetch_assoc($result)){
            $rows[] = $row;            
        }
        return $rows;   
    }

    function GetTotalVideo($data)
    {        
        $condition = "";
        if(!empty($data["keyword"])){
            $keyword = str_replace(" ", "%", $data["keyword"]);
            $condition .= " AND title LIKE ('%" . mysql_escape_string($keyword) . "%')";
        }
        $condition .= !empty($data["categoryId"]) ? " AND categoryId=" . $data["categoryId"] : "";
        $condition .= " AND (file_mp4 is not null OR file_mp4!='')";
        $condition .= " AND status=1";
        
        $sql = "SELECT count(id) FROM c_video WHERE 1 " . $condition;
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows["count(id)"];
    }
    
    function GetVideoOther($data)
    {    
        /* Lấy id tag của video hiện tại */
        $sql = "SELECT tagId FROM c_tag_data WHERE dataId=" . $data["videoId"] . " AND type=2";
        $result = @mysql_query($sql);
        $list_tag_id = "0";
        while($row = @mysql_fetch_assoc($result)){
            $list_tag_id .= "," . $row["tagId"];            
        }
        
        /* Lấy các video có cùng tag */
        $sql = "SELECT dataId FROM c_tag_data WHERE tagId IN (" . $list_tag_id . ") AND type=2 AND dataid!=" . $data["videoId"] . " LIMIT 5";
        $result = @mysql_query($sql);
        $list_video_id = "0";
        while($row = @mysql_fetch_assoc($result)){
            $list_video_id .= "," . $row["dataId"];            
        }
        
        $condition = "";
        $condition .= " AND (file_mp4 is not null OR file_mp4!='')";
        $condition .= $list_video_id!="0" ? " AND id IN (" . $list_video_id . ")" : "";
        $condition .= " AND id!=" . $data["videoId"];
        $condition .= " AND status=1";
        
        $sql = "SELECT id, title, introtext, download, picture, create_date, file, file_mp4, duration FROM c_video WHERE 1 " . $condition . " ORDER BY create_date DESC LIMIT 5";
        $result = @mysql_query($sql);
        $rows = array();
        while($row = @mysql_fetch_assoc($result)){
            $rows[] = $row;            
        }
        
        return $rows;   
    }
    
    /*
    * Lấy dữ liệu Album 
    */
    
    function GetAllAlbum($data)
    {
        $startRecord = ($data["p"] - 1) * $data["record"];
        $condition = "";
        if(!empty($data["keyword"])){
            $keyword = str_replace(" ", "%", $data["keyword"]);
            $condition .= " AND title LIKE ('%" . mysql_escape_string($keyword) . "%')";
        }         
        $condition .= !empty($data["categoryId"]) ? " AND categoryId=" . $data["categoryId"] : "";        
        
        $sql = "SELECT id, title, hit, picture, create_date FROM c_album WHERE 1 " . $condition . " ORDER BY update_date DESC LIMIT " . $startRecord . ", " . $data["record"];
        $result = @mysql_query($sql);
        $rows = array();
        
        $list_album_id = "0";
        while($row = @mysql_fetch_assoc($result)){
            $list_album_id .= ", " . $row["id"];
            $rows[] = $row;            
        }
        
        $sql = "SELECT count(id), albumId FROM c_image WHERE albumId IN (" . $list_album_id . ") GROUP BY albumId";
        $result = @mysql_query($sql);
        $replaceImage = array();        
        while($row = @mysql_fetch_assoc($result)){            
            $replaceImage[$row["albumId"]] = $row["count(id)"];
        }
        
        $i = 0;
        while(isset($rows[$i])){
            $rows[$i]["total_image"] = isset($replaceImage[$rows[$i]["id"]]) ? $replaceImage[$rows[$i]["id"]] : 0;
            $i++;
        }
        
        return $rows;   
    }

    function GetTotalAlbum($data)
    {        
        $condition = "";
        if(!empty($data["keyword"])){
            $keyword = str_replace(" ", "%", $data["keyword"]);
            $condition .= " AND title LIKE ('%" . mysql_escape_string($keyword) . "%')";
        }
        $condition .= !empty($data["categoryId"]) ? " AND categoryId=" . $data["categoryId"] : "";        
        
        $sql = "SELECT count(id) FROM c_album WHERE 1 " . $condition;
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows["count(id)"];
    }
    
    function GetAlbumOther($data)
    {    
        /* Lấy id tag của video hiện tại */
        $sql = "SELECT tagId FROM c_tag_data WHERE dataId=" . $data["albumId"] . " AND type=3";        
        $result = @mysql_query($sql);
        $list_tag_id = "0";
        while($row = @mysql_fetch_assoc($result)){
            $list_tag_id .= "," . $row["tagId"];            
        }
        
        /* Lấy các video có cùng tag */
        $sql = "SELECT dataId FROM c_tag_data WHERE tagId IN (" . $list_tag_id . ") AND type=3 AND dataId!=" . $data["albumId"] . " LIMIT 5";
        $result = @mysql_query($sql);
        $list_id = "0";
        while($row = @mysql_fetch_assoc($result)){
            $list_id .= "," . $row["dataId"];            
        }
        
        $condition = "";        
        $condition .= $list_id!="0" ? " AND id IN (" . $list_id . ")" : "";
        $condition .= " AND id!=" . $data["albumId"];
        
        $sql = "SELECT id, title, hit, picture, create_date FROM c_album WHERE 1 " . $condition . " ORDER BY update_date DESC LIMIT 5";
        $result = @mysql_query($sql);
        $rows = array();
        
        $list_album_id = "0";
        while($row = @mysql_fetch_assoc($result)){
            $list_album_id .= ", " . $row["id"];
            $rows[] = $row;            
        }
        
        $sql = "SELECT count(id), albumId FROM c_image WHERE albumId IN (" . $list_album_id . ") GROUP BY albumId";
        $result = @mysql_query($sql);
        $replaceImage = array();        
        while($row = @mysql_fetch_assoc($result)){            
            $replaceImage[$row["albumId"]] = $row["count(id)"];
        }
        
        $i = 0;
        while(isset($rows[$i])){
            $rows[$i]["total_image"] = isset($replaceImage[$rows[$i]["id"]]) ? $replaceImage[$rows[$i]["id"]] : 0;
            $i++;
        }
        
        return $rows;   
    }
    
    function GetImageByAlbum($data)
    {
        $sql = "SELECT id, file, create_date FROM c_image WHERE albumId=" . $data["albumId"];        
        $result = @mysql_query($sql);
        $rows = array();
        while($row = @mysql_fetch_assoc($result)){            
            $rows[] = $row;            
        }    
        return $rows;
    }
    
    /*
    * Lấy cú pháp sms
    */
    function getSmsSyntax()
    {
        $sql = "SELECT * FROM c_config_sms";        
        $result = @mysql_query($sql);
        $rows = array();
        while($row = @mysql_fetch_assoc($result)){            
            $rows[$row["type"]]["serviceCode"] = $row["serviceCode"];            
            $rows[$row["type"]]["syntax"] = $row["syntax"];            
        }    
        return $rows;   
    }
    
    function checkSmsLog($sms_content)
    {
        $sql = "SELECT count(id) FROM c_sms_log WHERE content='" . mysql_escape_string($sms_content) . "'";
        $result = @mysql_query($sql);
        $rows = @mysql_fetch_assoc($result);        
        return $rows["count(id)"];
    }
?>
